package com.gzs.zhdj.controller;

import com.gzs.zhdj.constant.RoleConstant;
import com.gzs.zhdj.domain.dto.PageBean;
import com.gzs.zhdj.domain.dto.PartyMemberPageDto;
import com.gzs.zhdj.domain.entity.PartyMember;
import com.gzs.zhdj.domain.dto.PartyMemberDto;
import com.gzs.zhdj.domain.dto.PasswordEditDto;
import com.gzs.zhdj.service.AdminService;
import com.gzs.zhdj.utils.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin")
public class
AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping("/import")
    public CommonResponse importUser(@RequestBody PartyMemberDto partyMemberDto) {
        log.info("导入党员, partyMemberDto:{}", partyMemberDto);
        adminService.importUser(partyMemberDto);
        return CommonResponse.ok();
    }

    @GetMapping("/selfinfo")
    public CommonResponse getPartyMember() {
        PartyMember partyMember = adminService.getSelfInfo();
        return CommonResponse.ok(partyMember);
    }
    @GetMapping("/getorgname")
    public CommonResponse getOrgName() {
        String orgName = adminService.getOrgName();
        return CommonResponse.ok(orgName);
    }
    @PutMapping("/updateselfinof")
    public CommonResponse update(@RequestBody PartyMember partyMember){
        log.info("修改党员信息, partyMember: {}", partyMember);
        adminService.updateSelfInof(partyMember);
        return CommonResponse.ok();
    }

    @PutMapping("/updatepassword")
    public CommonResponse updatepassword(@RequestBody PasswordEditDto passwordEditDto){
        log.info("修改密码, passwordEditDto: {}", passwordEditDto);
        adminService.updatePasswordById(passwordEditDto);
        return CommonResponse.ok();
    }

    @PutMapping("/updateinfobyid")
    public CommonResponse updateInfoById(@RequestBody PartyMember partyMember) {
        log.info("partyMember:{}", partyMember);
        adminService.updateInfoById(partyMember);
        return CommonResponse.ok();
    }

    @GetMapping("/{id}")
    public CommonResponse getUser(@PathVariable Integer id){
        log.info("根据ID查询党员信息, id: {}",id);
        PartyMember partyMember = adminService.getById(id);
        return CommonResponse.ok(partyMember);
    }
    @GetMapping("/getid")
    public CommonResponse getId(){
        List<Integer> idList = adminService.getId();
        return CommonResponse.ok(idList);
    }
    @GetMapping("/page")
    public CommonResponse page(PartyMemberPageDto partyMemberPageDto) {
        log.info("接收到分页查询请求：{}", partyMemberPageDto);
        try {
            PageBean pageBean = adminService.page(partyMemberPageDto);
            return CommonResponse.ok(pageBean);
        } catch (Exception e) {
            log.error("分页查询失败", e);
            return CommonResponse.fail("查询失败：" + e.getMessage());
        }
    }
    @PostMapping("/upload")
    public CommonResponse upload(MultipartFile image){
        adminService.upload(image);
        return CommonResponse.ok();
    }

}
